<template>
  <div class="wifiBox">
    <van-nav-bar safe-area-inset-top placeholder :title="$t('WifiConnection')" left-arrow fixed @click-left="onClickLeft">
      <template #right>
        <van-icon name="question-o" size="24" @click="openDiscription" />
      </template>
    </van-nav-bar>

    <van-form name="wifi" @submit="wificonn">
      <div class="registered_box">
        <div class="registered_li">
          <div class="registered_li_tit required">{{$t('RegistrationNo')}}</div>
          <div class="fieldBox">
            <van-field clearable name="wifisn" v-model="wifisn" type="text" />
            <div class="scan" @click="goCamera"></div>
          </div>
        </div>
      </div>
      <div class="btnBox">
        <van-button color="linear-gradient(#F9C25F, #F5861A)" :disabled="wifisn==''||wifisn==undefined" round block native-type="submit">{{$t('Next')}}</van-button>
      </div>
    </van-form>
    <wifisetting ref="discriptShow"></wifisetting>

    <div class="example">
      <img src="../assets/example.png" alt="">
    </div>
  </div>
</template>

<script>
import wifisetting from '../components/Protocol/WifiSetting'
export default {
  components:{
    wifisetting
  },
  data(){
    return {
      wifisn:this.$route.query.sn,
      isIOS:!!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    }
  },
  methods: {
    onClickLeft(){
      this.$router.push({path:'/main/user'});
    },
    openDiscription(){
      this.$refs.discriptShow.openPop(true,'1');
    },
    goCamera(){
      this.$router.push({path:'/camera',query:{code:3}});
    },
    wificonn(){
      let ssidNew="Wifi_"+this.wifisn;
      if(!this.isIOS){
        this.comm.loadingShow();
        // cordova.plugins.hotspot.getConnectionInfo((info)=>{
        //   cordova.plugins.hotspot.removeWifiNetwork(info.SSID,(info)=>{
            
        //   },(errorCB)=>{
        //     this.comm.toastHide();
        //     this.comm.tipsShow('WifiConnErr');
        //   })
        // },(errorCB)=>{
        //   this.comm.toastHide();
        //   this.comm.tipsShow('WifiConnErr');
        // });
        window.wifiManager.connect(ssidNew,"",(ssid, passphrase)=>{
            this.comm.toastHide();
            this.$router.push({path:'/wificonn'});
          },(code, message)=>{
            this.comm.toastHide();
            this.comm.tipsShow('WifiConnErr');
          }
        );
      }else{
        this.comm.loadingShow();
        window.wifiManager.connect(ssidNew,"",(ssid, passphrase)=>{
            this.comm.toastHide();
            this.$router.push({path:'/wificonn'});
          },(code, message)=>{
            this.comm.toastHide();
            this.comm.tipsShow('WifiConnErr');
          }
        );
      }
    }
  }
}
</script>

<style lang="less">
.wifiBox{
  .registered_box{
    width: 100%;
    background: #ffffff;
  }
  .registered_li{
    width: 100%;
    border-bottom: 1px solid #efefef;
  }
  .registered_li_tit{
    padding: 12px 0 0 15px;
    width: 100%;
    color: #929292;
    text-align: left;
    box-sizing: border-box;
  }
  .fieldBox{
    display: flex;
    justify-content: space-between;
    .scan{
      width: 50px;
      height: 50px;
      background: url('../assets/scan.png') no-repeat center / 30px;
    }
  }
  .example{
    width: 80%;
    margin: 40px auto;
    img{
      width: 100%;
      height: auto;
    }
  }
}
</style>
